/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.constant.SystemConstant;
import com.shishuo.cms.entity.Order;
import com.shishuo.cms.entity.User;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.OrderService;
import com.shishuo.cms.service.ProductService;
import com.shishuo.cms.service.UserService;
import com.shishuo.cms.util.HttpUtils;

@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction {

	final String path="/template/blog/"; 
	@Autowired
	private UserService userService;
	@Autowired
	private ProductService productService;
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/login.htm", method = RequestMethod.GET)
	public String userLogin(HttpServletRequest request, ModelMap modelMap) {
		return path+"login";
	}
	
	@RequestMapping(value = "/register.htm", method = RequestMethod.GET)
	public String userRegister(HttpServletRequest request, ModelMap modelMap) {
		return path+"register";
	}
	
	/**
	 * 
	 * 进入订单管理页面
	 */
	@RequestMapping(value = "/order/list.htm", method = RequestMethod.GET)
	public String order(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,HttpServletRequest request,
			ModelMap modelMap) {
		User user=(User) request.getSession().getAttribute(SystemConstant.SESSION_USER);
		if(user==null){
			return "redirect:" + HttpUtils.getBasePath(request);
		}
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("userId", user.getUserId());
		modelMap.put("pageVo", orderService.getAllList(map, pageNum));
		return path+"order";
	}
	
	@RequestMapping(value = "/toBuy.htm", method = RequestMethod.GET)
	public String toBuy(HttpServletRequest request,long productId,int num, ModelMap modelMap) {
		if (request.getSession().getAttribute(SystemConstant.SESSION_USER)==null) {
			return "redirect:" + HttpUtils.getBasePath(request);
		}
		modelMap.addAttribute("product",productService.getProductById(productId));
		modelMap.addAttribute("num",num);
		return path+"buy";
	}

	@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
	public String userLogout(HttpServletRequest request, ModelMap modelMap) {
		request.getSession().removeAttribute(SystemConstant.SESSION_USER);
		return "redirect:" + HttpUtils.getBasePath(request);
	}
	
	@ResponseBody
	@RequestMapping(value = "/toBuy.json", method = RequestMethod.POST)
	public JsonVo<String> toBuy(HttpServletRequest request,Order order) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			User user=(User) request.getSession().getAttribute(SystemConstant.SESSION_USER);
			order.setUserId(user.getUserId());
			order.setStatus(1);
			orderService.insert(order);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value = "/addUser.json", method = RequestMethod.POST)
	public JsonVo<String> addNewUser(
			@RequestParam(value = "name") String name,
			@RequestParam(value = "password") String password) {
		JsonVo<String> json = new JsonVo<String>();
		int user = userService.getUserByName(name);
		if (user>0) {
			json.getErrors().put("name", "用户名不能重复");
		}
		try {
			if (name.equals("")) {
				json.getErrors().put("name", "用户名不能为空");
			}
			if (StringUtils.isBlank(password)) {
				json.getErrors().put("password", "密码不能为空");
			} else if (password.length() < 6) {
				json.getErrors().put("password", "密码不能小于6位");
			} else if (password.length() > 16) {
				json.getErrors().put("password", "密码不能大于16位");
			}
			json.check();
			name.trim();
			userService.addUser(name, password);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/login.json", method = RequestMethod.POST)
	public JsonVo<String> userLogin(@RequestParam(value = "name") String name,
			@RequestParam(value = "password") String password,
			HttpServletRequest request, ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();

		try {
			if (StringUtils.isBlank(password)) {
				json.getErrors().put("password", "密码不能为空");
			} else if (password.length() < 6 && password.length() > 30) {
				json.getErrors().put("password", "密码最少6个字符，最多30个字符");
			}
			json.check();

			userService.UserLogin(name, password, request);

		} catch (Exception e) {
			json.setResult(false);
			json.getErrors().put("password", "邮箱或密码错误");
		}
		return json;
	}

}
